<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片地址" prop="productImage">
      <el-input v-model="dataForm.productImage" placeholder="商品图片地址"></el-input>
    </el-form-item>
    <el-form-item label="生成订单时的商品单价，单位是元,保留两位小数" prop="currentUnitPrice">
      <el-input v-model="dataForm.currentUnitPrice" placeholder="生成订单时的商品单价，单位是元,保留两位小数"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品总价,单位是元,保留两位小数" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="商品总价,单位是元,保留两位小数"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          orderNo: '',
          productId: '',
          productName: '',
          productImage: '',
          currentUnitPrice: '',
          quantity: '',
          totalPrice: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          productImage: [
            { required: true, message: '商品图片地址不能为空', trigger: 'blur' }
          ],
          currentUnitPrice: [
            { required: true, message: '生成订单时的商品单价，单位是元,保留两位小数不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '商品总价,单位是元,保留两位小数不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shop/goodsorderitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.goodsorderitem.userId
                this.dataForm.orderNo = data.goodsorderitem.orderNo
                this.dataForm.productId = data.goodsorderitem.productId
                this.dataForm.productName = data.goodsorderitem.productName
                this.dataForm.productImage = data.goodsorderitem.productImage
                this.dataForm.currentUnitPrice = data.goodsorderitem.currentUnitPrice
                this.dataForm.quantity = data.goodsorderitem.quantity
                this.dataForm.totalPrice = data.goodsorderitem.totalPrice
                this.dataForm.createTime = data.goodsorderitem.createTime
                this.dataForm.updateTime = data.goodsorderitem.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/goodsorderitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'orderNo': this.dataForm.orderNo,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'productImage': this.dataForm.productImage,
                'currentUnitPrice': this.dataForm.currentUnitPrice,
                'quantity': this.dataForm.quantity,
                'totalPrice': this.dataForm.totalPrice,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
